package com.client.service.impl;

import com.Emplyment.dto.ClientUserDto;
import com.Emplyment.dto.ReUserPasswordDto;
import com.Emplyment.entity.Comment;
import com.Emplyment.entity.InterView;
import com.Emplyment.entity.UserMessage;
import com.Emplyment.exception.BaseException;
import com.Emplyment.vo.*;
import com.client.mapper.UserCenterMapper;
import com.client.service.UserCenterService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class UserCenterServiceImpl implements UserCenterService {

    @Autowired
    private UserCenterMapper userCenterMapper;

    /**
     * 获取收藏的公司数据
     *
     * @param username
     * @return
     */
    @Override
    public List<ArticleVo> getCollectCompanys(String username) {
        List<ArticleVo> companyList = new ArrayList<>();
        //1.从数据库中获取用户收藏的所有公司id
        List<Integer> companyId = userCenterMapper.getCollectCompanyId(username);
        log.info("收藏的公司id为：{}", companyId);
        //2.根据查询到的公司id去公司表获取对应公司数据
        if (companyId.size() != 0) {
            companyList = userCenterMapper.getCompany(companyId);
        }
        return companyList;
    }

    /**
     * 获取收藏的问题数据
     *
     * @param username
     * @return
     */
    @Override
    public List<QuestionBankVo> getCollectQuestions(String username) {
        //1.从数据库中获取用户收藏的所有问题id
        List<QuestionBankVo> question = new ArrayList<>();
        List<Integer> questionId = userCenterMapper.getCollectQuestionId(username);
        log.info("收藏的问题id为：{}", questionId);
        //2.根据查询到的问题id去问题表获取对应问题数据
        if (questionId.size() != 0) {
            question = userCenterMapper.getQuestion(questionId);
        }
        return question;
    }

    @Override
    public void canalUserCompany(String username, Integer companyId) {
        //1.根据用户名与公司Id删除用户收藏的公司数据
        userCenterMapper.canalUserCompany(username, companyId);
        //2.公司数据的收藏数-1
        userCenterMapper.reduceCompanyCollect(companyId);
    }

    @Override
    public void canalUserQuestion(String username, Integer questionId) {
        //1.根据用户名与问题Id删除用户收藏的问题数据
        userCenterMapper.canalUserQuestion(username, questionId);
        //2.问题数据的收藏数-1
        userCenterMapper.reduceQuestionCollect(questionId);
    }

    /**
     * 更新用户信息
     *
     * @param clientUserDto
     */
    @Override
    public void updateUser(ClientUserDto clientUserDto) {
        if (clientUserDto.getNickname() == null && clientUserDto.getEmail() == null) {
            throw new BaseException("修改失败、没有要修改的值");
        }
        userCenterMapper.updateUser(clientUserDto);
    }

    /**
     * 更新用户头像
     *
     * @param avatarUrl
     */
    @Override
    public void updateAvatar(String username, String avatarUrl) {
        userCenterMapper.updateAvatar(username, avatarUrl);
    }

    /**
     * 重置密码
     *
     * @param reUserPasswordDto
     */
    @Override
    public void resetPassword(ReUserPasswordDto reUserPasswordDto) {
        String username = reUserPasswordDto.getUsername();
        UserMessage userMessage = userCenterMapper.checkUsername(username);
        String password = userMessage.getPassword();
        String oldPassword = DigestUtils.md5DigestAsHex(reUserPasswordDto.getOldPassword().getBytes());
        if (password.equals(oldPassword)) {
            String newPassword = DigestUtils.md5DigestAsHex(reUserPasswordDto.getNewPassword().getBytes());
            reUserPasswordDto.setNewPassword(newPassword);
            userCenterMapper.resetPassword(reUserPasswordDto);
        } else {
            throw new BaseException("原密码错误");
        }
    }

    /**
     * 获取用户发布的讨论
     *
     * @param username
     * @return
     */
    @Override
    public List<InterViewVo> getInterviewByUser(String username) {
        //根据用户名去查询对应讨论数据
        List<InterViewVo> interviewList = userCenterMapper.getInterviewByUser(username);
        log.info("用户{}发布的讨论数据为：{}", username, interviewList);
        if (interviewList.size() == 0) {
            interviewList = new ArrayList<>();
        }
        return interviewList;
    }

    /**
     * 删除用户发布的讨论
     */
    @Override
    public void deleteInterview(Integer interviewId) {
        //1.删除讨论数据
        userCenterMapper.deleteInterview(interviewId);
        log.info("删除讨论id为{}成功", interviewId);
        //2.删除评论数据
        userCenterMapper.deleteComment(interviewId);
    }

    /**
     * 获取用户发布的评论
     *
     * @param username
     * @return
     */
    @Override
    public List<UserComment> getCommentByUser(String username) {
        List<UserComment> userCommentList = new ArrayList<>();
        //1.根据用户名查询用户发布的评论数据
        List<CommentVo> commentList = userCenterMapper.getCommentByUser(username);
        log.info("用户{}发布的评论数据为：{}", username, commentList);
        //2.获取评论数据对应的讨论数据
        if (commentList.size() == 0) {
            userCommentList = new ArrayList<>();
        } else {
            for (CommentVo commentVo : commentList) {
                int interviewId = commentVo.getInterviewId();
                InterView interview = userCenterMapper.getInterviewById(interviewId);
                UserComment userComment = UserComment.builder()
                        .id(commentVo.getId())
                        .userPic(commentVo.getUserPic())
                        .content(commentVo.getContent())
                        .timestamp(commentVo.getTimestamp())
                        .interviewId(interviewId)
                        .author(interview.getAuthor())
                        .title(interview.getTitle())
                        .build();
                userCommentList.add(userComment);
            }
        }
        return userCommentList;
    }

    @Override
    public void deleteComment(String username, Integer interviewId,Integer commentId) {
        //1.根据用户名与讨论id删除用户发布的评论数据
        userCenterMapper.deleteCommentByUser(username, interviewId, commentId);
    }
}




